<?php

// 查立得PHP+mysql通用预约排队系统 V2024.12.12
// 演示地址: http://duilie.chalide.cn
// 文件路径: index.php
// 文件大小: 6507 字节
// 最后修改时间: 2024-12-17 21:00:00
// 作者: yujianyue
// 邮件: 15058593138@qq.com
// 版权所有,保留发行权和署名权
define('IN_SYSTEM', true);
require_once 'inc/conn.php';
require_once 'inc/pubs.php';
require_once 'inc/json.php';

// 获取医生ID
$doctor_id = isset($_GET['id']) ? intval($_GET['id']) : 0;

// 验证医生ID
$sql = "SELECT id, name, department, status, end_time FROM doctor WHERE id = $doctor_id";
$doctor = $conn->query($sql)->fetch_assoc();

if(!$doctor) {
    die('医生不存在:带ID参数'); //请带参数访问
}

// 处理AJAX请求
if(isset($_GET['act'])) {
    switch($_GET['act']) {
        case 'qrcode':
            refreshQRCode($doctor);
            break;
        case 'list':
            getWaitingList($doctor);
            break;
        default:
            json_msg(0, '未知操作');
    }
    exit;
}

// 刷新二维码
function refreshQRCode($doctor) {
    global $CONFIG;
    
    // 检查医生状态
    if($doctor['status'] != 1) {
        json_msg(0, '暂停预约');
    }
    
    // 检查结束时间
    if($doctor['end_time'] && strtotime($doctor['end_time']) < time()) {
        json_msg(0, '已结束预约');
    }
    
    // 生成时间戳
    $timestamp = time();
    
    // 生成二维码URL (使用时间戳确保36秒有效期)
    $url = "http://{$_SERVER['HTTP_HOST']}/dengji.php?id={$doctor['id']}&t=$timestamp";
    
    json_msg(1, 'success', array(
        'url' => $url,
        'doctor' => $doctor['name'],
        'department' => $doctor['department']
    ));
}

// 获取等待列表
function getWaitingList($doctor) {
    global $conn;
    $today = date('Y-m-d');
    $sql = "SELECT patient_name, sequence, department 
            FROM booking 
            WHERE doctor_id = {$doctor['id']} 
            AND booking_date = '$today' 
            AND status = 0 
            ORDER BY sequence ASC";          
    $result = $conn->query($sql); //问题出

    $list = array();
    while($row = $result->fetch_assoc()) {
        // 姓名显示首尾字
        $name = mb_substr($row['patient_name'], 0, 1) . '*' . 
                mb_substr($row['patient_name'], -1);
                
        $list[] = array(
            'name' => $name,
            'sequence' => $row['sequence'],
            'department' => $row['department']
        );
    }
    
    json_msg(1, 'success', $list);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $CONFIG['site_title']; ?></title>
    <style>
        body { 
            font-family: Arial; 
            max-width: 800px; 
            margin: 0 auto; 
            padding: 20px;
            text-align: center;
        }
        .qrcode {
            margin: 20px auto;
            padding: 20px;
            border: 1px solid #ddd;
            display: inline-block;
        }
        .info {
            margin: 10px 0;
            color: #666;
        }
        .list {
            margin-top: 30px;
            text-align: left;
        }
        .list-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
        }
        .error {
            color: red;
            padding: 20px;
        }
    </style>
</head>
<body>
    <h1><?php echo $CONFIG['site_name']; ?></h1>
    
    <div class="qrcode" id="qrcode">
        <img id="qrcode-img" src="" alt="二维码加载中...">
        <div class="info">
            <p>科室：<span id="department"></span></p>
            <p>医生：<span id="doctor"></span></p>
        </div>
    </div>
    
    <div class="list">
        <h3>当前等待</h3>
        <div id="waiting-list"></div>
    </div>

    <script src="inc/js.js"></script>
    <!-- 引入qrcode.js库用于生成二维码 -->
    <script src="./inc/Qrcode.js"></script>
    <script>
        // 刷新二维码
        function refreshQRCode() {
            ajax({
                url: 'index.php',
                type: 'GET',
                data: {
                    id: <?php echo $doctor_id; ?>,
                    act: 'qrcode'
                },
                success: function(res) {
                    var data = JSON.parse(res);
                    if(data.code === 1) {
                        // 生成二维码
                        var qr = qrcode(0, 'M');
                        qr.addData(data.data.url);
                        qr.make();
                        document.getElementById('qrcode-img').src = qr.createDataURL();
                        
                        // 更新信息
                        document.getElementById('department').textContent = data.data.department;
                        document.getElementById('doctor').textContent = data.data.doctor;
                    } else {
                        document.getElementById('qrcode').innerHTML = 
                            '<div class="error">' + data.msg + '</div>';
                    }
                }
            });
        }
        
        // 刷新等待列表
        function refreshList() {
            ajax({
                url: 'index.php',
                type: 'GET',
                data: {
                    id: <?php echo $doctor_id; ?>,
                    act: 'list'
                },
                success: function(res) {
                    var data = JSON.parse(res);
                    if(data.code === 1) {
                        var html = '';
                        data.data.forEach(function(item) {
                            html += '<div class="list-item">' +
                                   '序号：' + item.sequence + ' | ' +
                                   '姓名：' + item.name + ' | ' +
                                   '科室：' + item.department +
                                   '</div>';
                        });
                        document.getElementById('waiting-list').innerHTML = 
                            html || '<div class="list-item">暂无等待</div>';
                    }
                }
            });
        }
        
        // 初始刷新
        refreshQRCode();
        refreshList();
        
        // 定时刷新
        setInterval(refreshQRCode, <?php echo $CONFIG['qrcode_refresh'] * 1000; ?>);
        setInterval(refreshList, <?php echo $CONFIG['list_refresh'] * 1000; ?>);
    </script>
</body>
</html> 